package com.way;

/**
 * @author 魏彪
 */
public class Consumer extends AbstractConsumer {

    private final DataBuffer dataBuffer;

    public Consumer(DataBuffer dataBuffer) {
        this.dataBuffer = dataBuffer;
    }

    @Override
    protected void consume() {

        synchronized (dataBuffer.getDataBuffer()) {
            while (dataBuffer.getDataBuffer().isEmpty()) {
                try {
                    System.out.println("缓冲区空了，消费者等待");
                    dataBuffer.getDataBuffer().wait();
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
            Integer number = dataBuffer.getDataBuffer().remove(0);

            System.out.println("当前消费的对象是" + number);
            dataBuffer.getDataBuffer().notifyAll();
        }
    }
}
